package co.yixiang.modules.shop.service.impl;

import cn.hutool.core.util.IdUtil;
import co.yixiang.modules.shop.entity.YxInvestOrderCartInfo;
import co.yixiang.modules.shop.mapper.YxInvestOrderCartInfoMapper;
import co.yixiang.modules.shop.service.YxInvestOrderCartInfoService;
import co.yixiang.modules.shop.web.param.YxInvestOrderCartInfoQueryParam;
import co.yixiang.modules.shop.web.vo.YxInvestCartQueryVo;
import co.yixiang.modules.shop.web.vo.YxInvestOrderCartInfoQueryVo;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.web.vo.Paging;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 选购商品表 服务实现类
 * </p>
 *
 * @author hisen
 * @since 2020-06-11
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class YxInvestOrderCartInfoServiceImpl extends BaseServiceImpl<YxInvestOrderCartInfoMapper, YxInvestOrderCartInfo> implements YxInvestOrderCartInfoService {

    @Autowired
    private YxInvestOrderCartInfoMapper yxInvestOrderCartInfoMapper;

    @Autowired
    private YxInvestOrderCartInfoService yxInvestOrderCartInfoService;

    @Override
    public YxInvestOrderCartInfoQueryVo getYxInvestOrderCartInfoById(Serializable id) throws Exception{
        return yxInvestOrderCartInfoMapper.getYxInvestOrderCartInfoById(id);
    }

    @Override
    public Paging<YxInvestOrderCartInfoQueryVo> getYxInvestOrderCartInfoPageList(YxInvestOrderCartInfoQueryParam yxInvestOrderCartInfoQueryParam) throws Exception{
        Page page = setPageParam(yxInvestOrderCartInfoQueryParam,OrderItem.desc("create_time"));
        IPage<YxInvestOrderCartInfoQueryVo> iPage = yxInvestOrderCartInfoMapper.getYxInvestOrderCartInfoPageList(page,yxInvestOrderCartInfoQueryParam);
        return new Paging(iPage);
    }

    @Override
    public void saveCartInfo(Integer oid, List<YxInvestCartQueryVo> cartInfo) {
        List<YxInvestOrderCartInfo> list = new ArrayList<>();
        for (YxInvestCartQueryVo cart : cartInfo) {
            YxInvestOrderCartInfo info = new YxInvestOrderCartInfo();
            info.setOrderId(oid);
            info.setCartId(cart.getId().intValue());
            info.setProductId(cart.getProductId());
            info.setCartInfo(JSONObject.toJSON(cart).toString());
            info.setUnique(IdUtil.simpleUUID());
           // yxInvestOrderCartInfoService.save(info);
             list.add(info);
        }

         saveBatch(list);
    }

}
